package yl.hs.bmipfull.service.salemanage.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.salemanage.IContractControlInfoDao;
import yl.hs.bmipfull.pojo.salemanage.ContractControlInfoEntity;
import yl.hs.bmipfull.service.salemanage.IContractControlInfoService;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;

import java.util.List;
import java.util.Map;

/**
 * 作用:  销售合同发货控制信息服务层接口实现<br/>
 * 创建时间: 2021年05月19日 15时25分56秒 <br/>
 *
 * @author 张凯
 */
@Service
public class ContractControlInfoServiceImpl extends SaleBaseService implements IContractControlInfoService {

    @Autowired
    IContractControlInfoDao dao;


    @Override
    public List<ContractControlInfoEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(ContractControlInfoEntity.class);
        return dao.queryList(pageQuery.getQuery());
    }

    @Override
    public List<ContractControlInfoEntity> queryByContractSn(String contractSn) {
        return dao.queryByContractSn(contractSn);
    }


    @Override
    @Transactional()
    public Integer saveContractControlInfo(ContractControlInfoEntity controlInfoEntity) {
        Integer result = dao.insertNew(controlInfoEntity);
        if (result > 0) {
            writeLog(ActionType.CREATE, controlInfoEntity.getContractSN(), "插入销售合同控制信息", Tl.toJson(controlInfoEntity), ContractControlInfoEntity.class);
        }
        return result;
    }

    @Override
    public ContractControlInfoEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }


    @Override
    public Integer delete(String[] contractSns) {
        Integer result = 0;
        for (String s : contractSns) {
            result = +dao.delete(s);
        }

        return result;
    }

    @Override
    public Integer delete(String contractSn) {
        return dao.delete(contractSn);

    }

    @Override
    public Integer saveContractControlInfo(List<Map<String, Object>> controls, String contractSn) throws Exception {
        Integer result = 0;
        for (Map<String, Object> control : controls) {
            ContractControlInfoEntity entity = new ContractControlInfoEntity();
            Tl.getChangeLog(entity, control);
            entity.setContractSN(contractSn);
            result += saveContractControlInfo(entity);
        }
        return result;
    }


}